package me.atbar.moonlib.util;

import java.util.List;
import java.util.Random;

/**
 * @author Administrator
 */
public class RandomUtils {

    private RandomUtils() {}

    public static boolean hasChance(double chance) {
        return !(chance < 0 || chance > 100) && (Math.random() <= (chance / 100));
    }

    /**
     * 获得min 和 max 范围的一个数
     *
     * @param min 最小值
     * @param max 最大值
     * @return min 和 max 范围的一个数 (Contains min and max)
     */
    public static int getRandomInRange(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }


    /**
     * 生成max到min范围的浮点数
     */
    public static double nextDouble(final double min, final double max) {
        return min + ((max - min) * new Random().nextDouble());
    }

    /**
     * 生成指定长度的验证码
     *
     * @author Q
     * @param verificationCodeLength 验证码长度
     * @return String
     * @since 2016-09-6
     */
    public static String createVerificationCode(int verificationCodeLength) {
        String[] verificationCodeArray ={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        };
        StringBuilder verificationCode = new StringBuilder();
        Random random = new Random();
        for(int i = 0; i < verificationCodeLength; i++) {
            verificationCode.append(verificationCodeArray[random.nextInt(verificationCodeArray.length)]);
        }
        return verificationCode.toString();
    }

    public static String getTextFromList(List<String> list) {
        return list.get(new Random().nextInt(list.size()));
    }

}
